package au.com.bus4u.service.impl;

import au.com.bus4u.dao.DaoSupport;
import au.com.bus4u.service.RoleService;
import au.com.bus4u.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private DaoSupport daoSupport;
    private String mapper = "RoleMapper.";

    @Override
    public int save(PageData pd) throws Exception {
        pd.remove("role_id");
        pd.put("create_time",new Date());

        return (int) daoSupport.save(mapper+"save", pd);
    }

    @Override
    public int delete(Integer role_id) throws Exception {
//        PageData pd = new PageData();
//        pd.put("role_id",role_id);
//        return (int) daoSupport.delete(mapper+"deleteById", pd);
        return 0;
    }

    @Override
    public int deleteAll(String[] DATA_IDS) throws Exception {
//        return (int)daoSupport.batchDelete(mapper+"deleteAll", DATA_IDS);
        return 0;
    }

    @Override
    public int edit(PageData pd) throws Exception {
//        System.err.println("pd = " + pd);
        pd.put("update_time",new Date());
        int result = (int) daoSupport.update(mapper+"edit", pd);
//        System.out.println("result = " + result);
        return result;
    }

    @Override
    public List<PageData> list(PageData pd) throws Exception {

        if(pd.get("role_id") == null || "".equals(pd.get("role_id"))){
            pd.remove("role_id");
        }
        if(pd.get("role_name") == null || "".equals(pd.get("role_name"))){
            pd.remove("role_name");
        }
        if(pd.get("description") == null || "".equals(pd.get("description"))){
            pd.remove("description");
        }
        if(pd.get("status") == null || "".equals(pd.get("status"))){
            pd.remove("status");
        }
        if(pd.get("create_time") == null || "".equals(pd.get("create_time"))){
            pd.remove("create_time");
        }
        if(pd.get("update_time") == null || "".equals(pd.get("update_time"))){
            pd.remove("update_time");
        }


        List<PageData> list = (List<PageData>) daoSupport.findForList(mapper+"list",pd);
//        System.out.println("list = " + list);
        return list;
    }

    @Override
    public PageData findById(Integer role_id) throws Exception {
        PageData pd = new PageData();
        pd.put("role_id",role_id);
        return (PageData) daoSupport.findForObject(mapper+"findById", pd);
    }

    /**
     * 根据角色ID查询当前角色拥有的所有的权限或菜单ID
     */
    @Override
    public List<Integer> queryRolePermissionIdsByRid(Integer roleId) {
        return queryRolePermissionIdsByRid(roleId);
    }

    /**
     * 保存角色和菜单权限之间的关系
     */
    @Override
    public void saveRolePermission(Integer rid, Integer[] ids) {
//        RoleMapper roleMapper = this.getBaseMapper();
//        //根据rid删除sys_role_permission
//        roleMapper.deleteRolePermissionByRid(rid);
//        if(ids!=null&&ids.length>0) {
//            for (Integer pid : ids) {
//                roleMapper.saveRolePermission(rid,pid);
//            }
//        }

    }

    /**
     * 查询当前用户拥有的角色ID集合
     */
    @Override
    public List<Integer> queryUserRoleIdsByUid(Integer id) {

//        return this.getBaseMapper().queryUserRoleIdsByUid(id);
        return null;
    }
}
